﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();

            this.webBrowser2.Navigate("http://data.shishicai.cn/cqssc/haoma/");

            this.webBrowser2.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);

            timer1.Interval = 20000;//定时时间

            timer1.Start();
        }


        int count = 0;

        void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

            count = 0;

            HtmlDocument doc = this.webBrowser2.Document;
            HtmlElementCollection hecs = doc.GetElementsByTagName("a");
            foreach (HtmlElement aElement in hecs)
            {
                if (aElement.InnerText == "进入旧版")
                {
                    aElement.InvokeMember("click");
                }

            }


            for (int j = 1; j < doc.Body.GetElementsByTagName("table")[1].GetElementsByTagName("tr").Count; j++)
            {
                if (j == 2)
                {
                    j++;
                }
                string strValue = doc.Body.GetElementsByTagName("table")[1].GetElementsByTagName("tr")[j].InnerText;

                string strQs = strValue.Substring(0, 12);

                string strNUM = strValue.Substring(12, 9).ToString().Replace(' ', '-');

                string strDate = strValue.Substring(21, 16);

                this.dataGridView1.Rows.Add();

                this.dataGridView1.Rows[count].Cells[0].Value = count.ToString();//期数

                this.dataGridView1.Rows[count].Cells[1].Value = strQs.ToString();//期数

                this.dataGridView1.Rows[count].Cells[2].Value = strNUM.ToString();///号码

                this.dataGridView1.Rows[count].Cells[3].Value = strDate.ToString(); ///时间

                count++;
            }

            Checkone(0, 1, 2, label2, label4, label6, label8, label10, label12, label14, label16, label18, label20);
            Checkone(2, 3, 4, label40, label38, label34, label36, label30, label32, label26, label28, label22, label24);

            // Checktwo(0, 1, 2, label2, label4, label6, label8, label10, label12, label14, label16, label18, label20);

            //Checkone(4, 2, label140, label138, label136, label134, label132, label130, label128, label126, label124, label122);
            //Checkone(3, 2, label1, label14, label20, label16, label18, label30, label28, label22, label26, label24);
            //Checkone(4, 3, label50, label48, label46, label44, label42, label40, label38, label36, label34, label32);
            //Checkone(3, 1, label70, label68, label66, label64, label62, label60, label58, label56, label54, label52);
            //Checkone(1, 0, label90, label88, label86, label84, label82, label80, label78, label76, label74, label72);
            //Checkone(2, 0, label110, label108, label106, label104, label102, label100, label98, label96, label94, label92);
            //Checkone(2, 1, label120, label118, label116, label114, label112, label11, label9, label7, label5, label3);



            //dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Transparent;
        }



        /// <summary>
        /// 定时读取新数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            ClearCookie(this.webBrowser2);

            dsd();

        }

        public void dsd()
        {
            this.webBrowser2.Refresh();

            this.webBrowser2.Navigate("http://data.shishicai.cn/cqssc/haoma/");

        }

        public void Checkone(int s1, int s2, int s3, Label lab0, Label lab1, Label lab2, Label lab3, Label lab4, Label lab5, Label lab6, Label lab7, Label lab8, Label lab9)
        {

            Label[] labelFirst = new Label[10];
            labelFirst[0] = lab0; //第一个五位期数
            labelFirst[1] = lab1; //第二个五位期数
            labelFirst[2] = lab2; //第三个五位期数
            labelFirst[3] = lab3; //第四个五位期数
            labelFirst[4] = lab4; //第五个五位期数
            labelFirst[5] = lab5; //第一个五位期数
            labelFirst[6] = lab6; //第二个五位期数
            labelFirst[7] = lab7; //第三个五位期数
            labelFirst[8] = lab8; //第四个五位期数
            labelFirst[9] = lab9; //第五个五位期数


            int i = 0;
            int j = 0;
            for (j = 0; j < 10; j++)
            {
                labelFirst[j].Text = "";
                int m = 0, n = 0;
                int a7 = 0;
                for (i = 0; i < 120; i++)///获取的是列表的总行数
                {
                    string first = this.dataGridView1.Rows[i].Cells[2].Value.ToString();//取号码数据

                    int a1 = Int32.Parse(first.Split('-')[s1].ToString());//取m行第一个数据

                    int a2 = Int32.Parse(first.Split('-')[s2].ToString());

                    int a3 = Int32.Parse(first.Split('-')[s3].ToString());



                    int a4 = a1 - a2;

                    int a5 = a2 - a3;

                    int a6 = a1 - a3;

                    if (a4 < 0)
                        a4 += 10;
                    if (a5 < 0)
                        a5 += 10;
                    if (a6 < 0)
                        a6 += 10;

                    if ((a4 == j) || (a5 == j) || (a6 == j))
                    {

                        labelFirst[j].Text += m.ToString() + " ";
                        m = 0;
                    }
                    else
                        m++;

                }
            }




        }

        //----------------------清COOK方法----------------------------------------

        public void ClearCookie(WebBrowser browser)
        {
            browser.Navigate("javascript:void((function(){var a,b,c,e,f;f=0;a=document.cookie.split('; ');for(e=0;e<a.length&&a[e];e++){f++;for(b='.'+location.host;b;b=b.replace(/^(?:%5C.|[^%5C.]+)/,'')){for(c=location.pathname;c;c=c.replace(/.$/,'')){document.cookie=(a[e]+'; domain='+b+'; path='+c+'; expires='+new Date((new Date()).getTime()-1e11).toGMTString());}}}})())");
        }



        private void button1_Click(object sender, EventArgs e)
        {
            int s1 = int.Parse(textBox1.Text.Trim());

            int i, j, k;
            int s3 = 0;
            int[,] num1 = new int[300, 3];
            for (i = 0; i < 10; i++)
            {
                for (j = 0; j < 10; j++)
                {
                    for (k = 0; k < 10; k++)
                    {
                        int s4 = i - j;
                        int s5 = i - k;
                        int s6 = j - k;
                        if (s4 < 0)
                            s4 += 10;
                        if (s5 < 0)
                            s5 += 10;
                        if (s6 < 0)
                            s6 += 10;
                        if ((s4 == s1) || (s5 == s1) || (s6 == s1))
                        {
                            num1[s3, 0] = i;
                            num1[s3, 1] = j;
                            num1[s3, 2] = k;
                            s3++;
                        }
                    }
                }
            }

            StreamWriter sw1 = new StreamWriter("ss.txt");
            StringBuilder sb1 = new StringBuilder();
            for (i = 0; i < s3; i++)
            {

                sb1.Append(num1[i, 0].ToString());
                sb1.Append(num1[i, 1].ToString());
                sb1.Append(num1[i, 2].ToString());
                sb1.Append(System.Environment.NewLine);
            }
            sw1.Write(sb1.ToString());
            sw1.Close();


            System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();

            info.WorkingDirectory = Application.StartupPath;

            info.FileName = System.AppDomain.CurrentDomain.BaseDirectory + "ss.txt";

            System.Diagnostics.Process.Start(info);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            int s1 = int.Parse(textBox2.Text.Trim());

            int i, j, k;
            int s3 = 0;
            int[,] num1 = new int[300, 3];
            for (i = 0; i < 10; i++)
            {
                for (j = 0; j < 10; j++)
                {
                    for (k = 0; k < 10; k++)
                    {
                        int s4 = i - j;
                        int s5 = i - k;
                        int s6 = j - k;
                        if (s4 < 0)
                            s4 += 10;
                        if (s5 < 0)
                            s5 += 10;
                        if (s6 < 0)
                            s6 += 10;
                        if ((s4 == s1) || (s5 == s1) || (s6 == s1))
                        {
                            num1[s3, 0] = i;
                            num1[s3, 1] = j;
                            num1[s3, 2] = k;
                            s3++;
                        }
                    }
                }
            }

            StreamWriter sw1 = new StreamWriter("ss.txt");
            StringBuilder sb1 = new StringBuilder();
            for (i = 0; i < s3; i++)
            {

                sb1.Append(num1[i, 0].ToString());
                sb1.Append(num1[i, 1].ToString());
                sb1.Append(num1[i, 2].ToString());
                sb1.Append(System.Environment.NewLine);
            }
            sw1.Write(sb1.ToString());
            sw1.Close();


            System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();

            info.WorkingDirectory = Application.StartupPath;

            info.FileName = System.AppDomain.CurrentDomain.BaseDirectory + "ss.txt";

            System.Diagnostics.Process.Start(info);
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            Task.Factory.StartNew(() =>
            {
                var data = Program.GetData(DateTime.Now);//今天的数据
                data.AddRange(Program.GetData(DateTime.Now.AddDays(-1)));//昨天 
                data.AddRange(Program.GetData(DateTime.Now.AddDays(-2)));//前天
                int index = 1;
                data.ForEach(x => x.ID = index++);
                this.Invoke(new Func<List<LotteryData>, bool>(x => { this.lotteryDataBindingSource.DataSource = x; return true; }), data);
            });
        }
    }
}
